<template>
  <el-tree :data="data" node-key="id" :default-expanded-keys="defaultExpandedKeys"
    :default-checked-keys="defaultCheckedKeys" @node-click="handleNodeClick"></el-tree>
</template>

<script>
import { getPageData } from "@/api/website";
import { Number } from "core-js";
import { mount } from "sortablejs";
import $ from 'jquery';
export default {
  props: {
    treeIdChecked: {
      type: Number,
      default: ''
    },
    childIdChecked: {
      type: Number,
      default: ''
    }
  },
  data() {
    return {
      data: [],
      defaultExpandedKeys: [3, 8],
      defaultCheckedKeys: [18]
    };
  },
  created() {
    this.init();
    if(this.$route.query.id){
      this.defaultCheckedKeys = [parseFloat(this.$route.query.id)]
    }else{
      this.defaultCheckedKeys = [parseFloat(this.childIdChecked)]
    }


  },
  mounted() {
    // this.$nextTick(() => {
    //   setTimeout(() => {
    //     let firstNode = this.$refs.tree.getCheckedNodes()[0];
    //     if (firstNode) {
    //       this.$refs.tree.setChecked(firstNode.id, true);
    //     }
    //   }, 0);
    // });

  },
  methods: {
    handleNodeClick(data) {
      $(".el-tree-node").removeClass("is-checked")
      if (!data.children || data.children.length == 0) {
        this.$emit('node-click', data.id);
      }
    },
    init() {
      getPageData({ id: this.treeIdChecked }).then(response => {
        let data = response.data.childrenCategory;
        let array = [];
        for (const key in data) {
          array.push({ label: data[key].name, id: data[key].id, children: [] })
          if (data[key].childrenCategory) {
            for (const seckey in data[key].childrenCategory) {
              array[key].children.push({ label: data[key].childrenCategory[seckey].name, id: data[key].childrenCategory[seckey].id, children: [] })
              if (data[key].childrenCategory[seckey].childrenCategory) {
                for (const thrkey in data[key].childrenCategory[seckey].childrenCategory) {
                  array[key].children[seckey].children.push({ label: data[key].childrenCategory[seckey].childrenCategory[thrkey].name, id: data[key].childrenCategory[seckey].childrenCategory[thrkey].id })
                }
              }

            }
          }
        }
        this.data = array;
      });
    }
  }
};
</script>

<style lang="scss"></style>
